package com.smart.wap.app;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.smart.common.entities.ajax.AjaxResult;
import com.smart.common.entities.constant.SmartConstant;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.enums.SmsScene;
import com.smart.manager.member.entities.assembler.MemberSimple;
import com.smart.manager.member.entities.bodys.RMemberMaterialCombinePig;
import com.smart.manager.member.entities.models.*;
import com.smart.manager.member.repository.MemberAccountRepository;
import com.smart.manager.member.repository.MemberMaterialPigCombineRepository;
import com.smart.manager.member.repository.MemberMaterialPigRepository;
import com.smart.manager.member.repository.MemberProfileMainRepository;
import com.smart.manager.product.entities.models.PigGoods;
import com.smart.manager.weixin.mp.handler.WxmpQrcodeHandler;
import com.smart.service.sms.SmsService;
import com.smart.utils.MemberAcl;
import com.smart.utils.MemberStp;
import com.smart.wap.bodys.LoginBody;
import com.smart.wap.bodys.WapMemberBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

/**
 * 会员资料信息接口
 *
 * @author Administrator
 */
@Api(tags = "移动端-会员资料")
@RestController
@RequestMapping(SmartConstant.WAP_API_PREFIX + "app/member")
@RequiredArgsConstructor
@Log4j2
public class MemberController {

    private final MemberProfileMainRepository profileRepository;
    private final MemberAccountRepository accountRepository;
    private final MemberMaterialPigCombineRepository memberMaterialPigCombineRepository;
    private final SmsService smsService;
    private final MemberMaterialPigRepository memberMaterialPigRepository;
    private final WxmpQrcodeHandler wxmpQrcodeHandler;

    @ApiOperation(value = "获取会员基本资料")
    @GetMapping(value = "myInfo")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<MemberSimple> getMyInfo() {
        MemberSimple result = profileRepository.readSimple(MemberAcl.getLoginId(), true);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "设置用户新手指引")
    @GetMapping(value = "addGuideline/{msg}")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> addGuidelines(@PathVariable String msg) {
        profileRepository.addGuidelines(MemberAcl.getLoginId(), msg);
        return AjaxResult.success();
    }


    @ApiOperation(value = "获取企业会员猪仔信息")
    @GetMapping(value = "getGoodsPig")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<PigGoods> getGoodsPig() {
        PigGoods goods = profileRepository.getDefaultGoods();
        return AjaxResult.data(goods);
    }

    @ApiOperation(value = "企业会员领取猪仔")
    @GetMapping(value = "addDefaultGoods")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<MemberMaterialPig> addDefaultGoods() {
        MemberMaterialPig materialPig = profileRepository.addDefaultGoods();
        return AjaxResult.data(materialPig);
    }


    @ApiOperation(value = "提交合养申请")
    @PostMapping(value = "AddCombinePigInfo")
    @SaCheckLogin(type = MemberStp.TYPE)
    @ApiOperationSupport(order = 2)
    public AjaxResult<String> AddCombinePigInfo(@RequestBody @Validated RMemberMaterialCombinePig.AddBody body) {
        Long loginId = MemberAcl.getLoginId();
        memberMaterialPigCombineRepository.addCombine(loginId, body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取合养申请")
    @GetMapping(value = "getCombinePigInfo")
    @SaCheckLogin(type = MemberStp.TYPE)
    @ApiOperationSupport(order = 1)
    public AjaxResult<List<MemberMaterialCombinePig>> getCombinePigInfo() {
        Long loginId = MemberAcl.getLoginId();
        List<MemberMaterialCombinePig> info = memberMaterialPigCombineRepository.getCombinePigInfo(loginId);
        return AjaxResult.data(info);
    }

    @ApiOperation(value = "处理合养请求")
    @PostMapping(value = "dispose")
    @SaCheckLogin(type = MemberStp.TYPE)
    @ApiOperationSupport(order = 3)
    public AjaxResult<String> getCombinePigInfo(@RequestBody @Validated RMemberMaterialCombinePig.DisposeBody body) {
        Long loginId = MemberAcl.getLoginId();
        memberMaterialPigCombineRepository.dispose(loginId, body);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取会员货币账户")
    @GetMapping(value = "getMyAccount")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<MemberProfileAccount> getMyAccount() {
        Long loginId = MemberAcl.getLoginId();
        MemberProfileAccount result = accountRepository.getAccount(loginId);
        return AjaxResult.data(result);
    }

    @ApiOperation(value = "获取会员备用金")
    @GetMapping(value = "getReserve")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<Integer> getReserve() {
        Long loginId = MemberAcl.getLoginId();
        Integer result = memberMaterialPigRepository.getReserve(loginId);
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "获取金币动态列表")
    @GetMapping(value = "getMyCoinBill")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<Page<MemberCoinBill>> getMyCoinBill(@Validated BaseSearch search) {
        Long memberId = MemberAcl.getLoginId();
        Page<MemberCoinBill> result = accountRepository.getMyCoinBillPage(memberId, search);
        return AjaxResult.data(result);
    }


    @ApiOperation(value = "修改昵称")
    @PostMapping(value = "changeNickName")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> changeNickName(@Validated @RequestBody WapMemberBody.ChangeNameBody body) {
        profileRepository.changeNickName(MemberAcl.getLoginId(), body.getNickName());
        return AjaxResult.success();
    }


    @ApiOperation(value = "修改头像")
    @PostMapping(value = "changAvatar")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> changAvatar(@Validated @RequestBody WapMemberBody.ChangeAvatarBody body) {
        profileRepository.changeAvatar(MemberAcl.getLoginId(), body.getAvatar());
        return AjaxResult.success();
    }


    @ApiOperation(value = "注销1-获取验证码")
    @PostMapping(value = "getSignOutCode")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> getSignOutCode(@RequestBody @Validated LoginBody.LoadCaptchaBody body) {
        body.checkSign();
        MemberProfileMain profileMain = profileRepository.getOneById(MemberAcl.getLoginId());
        Assert.equals(profileMain.getPhoneNumber(), body.getPhoneNumber(), "登录的手机号与要注销的手机号不一致 无法进行注销操作");
        smsService.sendCaptchaCode(SmsScene.MEMBER_SIGN_OUT, body.getPhoneNumber());
        return AjaxResult.success();
    }


    @ApiOperation(value = "注销2-提交验证")
    @PostMapping(value = "actionSignOut")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> actionSignOut(@RequestBody @Validated WapMemberBody.SignOutBody body) {
        Long memberId = MemberAcl.getLoginId();
        // 验证验证码
        smsService.checkCaptchaCode(SmsScene.MEMBER_SIGN_OUT, body.getPhoneNumber(), body.getCaptcha());
        //提交账户注销程序
        profileRepository.actionSignOut(memberId);
        //处理账户下线程序
        MemberAcl.loginOut(memberId);
        return AjaxResult.success("账户注销成功");
    }


    @ApiOperation(value = "获取公众号二维码")
    @GetMapping(value = "getBindQrcode")
    @SaCheckLogin(type = MemberStp.TYPE)
    public AjaxResult<String> getBindQrcode() throws WxErrorException {
        Long memberId = MemberAcl.getLoginId();
        String content = SmartConstant.BING_ACCOUNT + memberId;
        String result = wxmpQrcodeHandler.getQrCodeStrFormDefault(content);
        QrConfig qrConfig = QrConfig.create()
                .setBackColor(new Color(255,255,255,0))
                .setForeColor(new Color(108,55,23))
                .setWidth(300).setHeight(300).setMargin(0);


        String s = QrCodeUtil.generateAsBase64(result ,qrConfig, ImgUtil.IMAGE_TYPE_PNG);
        return AjaxResult.data(s);
    }


}
